package com.hnust.root.web.controller.forward.user;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnust.root.common.base.response.BaseRestController;
import com.hnust.root.common.base.response.BaseResult;
import com.hnust.root.common.base.response.PageData;
import com.hnust.root.dao.entity.domain.forward.*;
import com.hnust.root.service.forword.wx.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/group")
public class TopicController extends BaseRestController {
    private String[] strings = {"恶意攻击谩骂", "广告营销", "含敏感词汇", "包含色情服务"};

    @Autowired
    private TopicService topicService;

    @Autowired
    private UserLikeService userLikeService;

    @PostMapping("/addTopic")
    @ApiOperation("发布话题")
    public BaseResult<Topic> addTopic(@RequestBody Topic topic) {
        topic = topicService.addTopic(topic);
        return topic != null ? fillSuccessData(topic) : fillFailData("发布失败");
    }

    @ApiOperation("根据学校分页获取话题,通过发布时间逆序")
    @PostMapping("/getTopicList")
    public BaseResult<PageData> getTopicList(@RequestBody JSONObject jsonObject) {
        int start = jsonObject.getInt("start");
        int limit = jsonObject.getInt("limit");
        String university = jsonObject.getStr("university");

        IPage<Topic> page = new Page<>(start, limit);
        page = topicService.getTopTopicList(page, university);
        return fillSuccessData(new PageData(page));
    }


    @ApiOperation("通过话题ID获取话题")
    @PostMapping("/getTopicById")
    public BaseResult<Topic> getTopicById(@RequestBody JSONObject jsonObject) {
        int topicId = jsonObject.getInt("topicId");
        return fillSuccessData(topicService.getTopicById(topicId));
    }

    @ApiOperation("通过ID删除话题")
    @PostMapping("/deleteTopic")
    public BaseResult deleteTopic(@RequestBody JSONObject jsonObject) {
        int id = jsonObject.getInt("id");
        boolean res = topicService.deleteTopicById(id);
        return res ? fillSuccessData() : fillFailData();
    }

    @ApiOperation("举报话题")
    @PostMapping("/postTipOffsTopic")
    public BaseResult tipOffTopic(@RequestBody JSONObject jsonObject) {
        int topId = jsonObject.getInt("topicId");
        int tipOff = jsonObject.getInt("type");
        Topic topic = topicService.getTopicById(topId);

        //设置为已被举报状态
        topic.setStatus(2);
        //被举报原因
        topic.setTipOff(strings[tipOff]);
        boolean res = topicService.updateTopicById(topic);
        return res ? fillSuccessData() : fillFailData();
    }

    @ApiOperation("通过用户的openId获取用户的话题列表")
    @PostMapping("/getTopicListByUserId")
    public BaseResult<List> getTopicListByUserId(@RequestBody JSONObject jsonObject) {
        String openId = jsonObject.getStr("openId");
        List<Topic> topicList = topicService.getTopicListByUserId(openId);
        return fillSuccessData(topicList);
    }

    @ApiOperation("用户点赞话题")
    @PostMapping("/addLiked")
    public BaseResult addLiked(@RequestBody JSONObject jsonObject) {
        boolean liked = jsonObject.getBool("liked");
        int topicId = jsonObject.getInt("topicId");
        UserLike like = new UserLike(getWxAccountOpenId(), topicId, liked);
        boolean result = userLikeService.userLike(like);
        return result ? fillSuccessData() : fillFailData();
    }

    @ApiOperation("通过学校或者大学获取banner")
    @PostMapping("/getBannerList")
    public BaseResult<List<Banner>> getBannerList() {
        return fillSuccessData(topicService.getBanner(getWxAccountOpenId()));
    }
}
